<?php
/**
 * Created by PhpStorm.
 * User:tsingStar
 * Date: 2019/7/13
 * Time: 19:00
 */

namespace app\home\controller;


use app\common\model\InvitePoster;
use app\common\model\Settings;

class Commission extends Common
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 分销中心
     */
    public function index()
    {

        $this->templateTitle("分销中心");

        $user = \app\common\model\User::exists(session("user_id"));
        //拉取上级
        $parent = \app\common\model\User::exists($user['parent_id']);
        if (empty($parent)) {
            $parent_name = "平台";
        } else {
            $parent_name = $parent["nick_name"];
        }
        $this->assign("parent_name", $parent_name);
        $this->assign("user", $user);
        //分销佣金
        $total_commission = \app\common\model\Commission::getCommissionTotal(session("user_id"));
        $this->assign("total_commission", $total_commission);
        //分销订单
        $order_commission = \app\common\model\Commission::getCommissionOrder(session("user_id"));
        $this->assign("order_commission", $order_commission);
        //提现明细
        $this->assign("withdraw_num", \app\common\model\Commission::getWithdrawLogNum(session("user_id")));
        //我的下线
        $children = \app\common\model\Commission::getTotal(session("user_id"));
        $c_count = count($children["children1"] + $children["children2"]);
        $this->assign("c_count", $c_count);

        return $this->fetch("index");
    }

    /**
     * 邀请好友
     */
    public function invite()
    {
        $this->templateTitle("邀请好友");
        //生成邀请二维码
        $user = \app\common\model\User::exists(session("user_id"));
        $img_url = InvitePoster::spread($user);
        $this->assign("img_url", $img_url);
        return $this->fetch("invite");
    }

    public function commission()
    {
        $this->templateTitle("分销佣金");
        $rate = db("rate")->find();
        $this->assign("rate", $rate);
        $this->assign("user", \app\common\model\User::exists(session("user_id")));
        //分销佣金
        $total_commission = \app\common\model\Commission::getCommissionTotal(session("user_id"));
        $this->assign("total_commission", $total_commission);
        //未收货佣金
        $this->assign("no_commission", \app\common\model\Commission::getCommissionNoCal(session("user_id")));
        //已申请佣金
        $apply_commission = db("user_withdraw")->where("status", 0)->where("uid", session("user_id"))->sum("money");
        $this->assign("apply_commission", $apply_commission);
        //待打款佣金
        $ready_pay = db("user_withdraw")->where("status", 1)->where("uid", session("user_id"))->sum("money");
        $this->assign("ready_pay", $ready_pay);
        //无效佣金
        $cancel_commission = db("user_withdraw")->where("status", 3)->where("uid", session("user_id"))->sum("money");
        $this->assign("cancel_commission", $cancel_commission);
        return $this->fetch("commission");
    }

    /**
     * 提现明细
     */
    public function withdrawLog()
    {
        if (request()->isAjax()) {
            $type = input("type");
            $page = input("page");
            $page_num = input("page_num");
            $list = \app\common\model\Commission::getWithdrawLog(session("user_id"), $page, $page_num, $type);
            foreach ($list as &$item) {
                $item["create_time"] = date("Y-m-d H:i:s");
            }
            if (empty($list)) {
                $this->ajaxSuccess("暂无记录", $list);
            } else {
                $this->ajaxSuccess("请求成功", $list);
            }

        }
        $this->templateTitle("提现明细");
        return $this->fetch("withdraw_log");
    }

    /**
     * 下线
     */
    public function member()
    {
        $this->templateTitle("我的下线");
        $member = \app\common\model\Commission::getTotal(session("user_id"));
        $member1 = db("user")->where("id", "in", $member["children1"])->select();
        $member2 = db("user")->where("id", "in", $member["children2"])->select();
        $this->assign("member1", $member1);
        $this->assign("member2", $member2);
        $this->assign("member", $member);

        return $this->fetch("member");
        
    }

    /**
     * 分销订单
     */
    public function commissionOrder()
    {
        $this->templateTitle("分销订单");
        if(request()->isAjax()){
            $type = input("type");
            $page = input("page");
            $page_num = input("page_num");
            $list = \app\common\model\Commission::getOrder(session("user_id"), $type, $page, $page_num);
            if(empty($list)){
                $this->ajaxSuccess("暂无订单",[]);
            }else{
                $this->ajaxSuccess("请求成功",$list);
            }
        }

        return $this->fetch("commission_order");

    }

    /**
     * 提现
     */
    public function withdraw()
    {
        $this->templateTitle("提现");
        $user = \app\common\model\User::exists(session("user_id"));
        $this->assign("user", $user);
        $rate = db("rate")->find();
        $this->assign("rate", $rate);
        if(request()->isAjax()){
            $data = input("post.");
            if(empty($data["user_name"]) ||empty($data["bank_num"]) ||empty($data["bank_name"])){
                $this->ajaxError("提现账户信息不完善");
            }
            if($data["com_ready"]<$rate["user_withdraw_min"]){
                $this->ajaxError("提现金额不足最低提现额");
            }
            db("user_withdraw")->insert([
                "uid"=>session("user_id"),
                "money"=>$data["com_ready"],
                "fee"=>round($data["com_ready"]*$rate["withdraw_rate"]/100, 2),
                "act_money"=>$data["com_ready"]-round($data["com_ready"]*$rate["withdraw_rate"]/100, 2),
                "create_time"=>time(),
                "update_time"=>time(),
                "bank_name"=>$data["bank_name"],
                "bank_num"=>$data["bank_num"],
                "user_name"=>$data["user_name"]
            ]);
            //$user_id, $commission, $type, $content, $order_id = 0
            \app\common\model\Commission::commissionLog(session("user_id"), $data["com_ready"], 2, "用户提现");
            db("user")->where("id", session("user_id"))->setDec("commission_ready", $data["com_ready"]);
            $this->ajaxSuccess("提现申请已提交");
        }
        return $this->fetch("withdraw");
    }

}